View Javadoc

1   // PendingBuffer.java, created Mon Jul  8  4:06:18 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   
5   package joeq.ClassLib.Common.java.util.zip;
6   
7   /***
8    * PendingBuffer
9    *
10   * @author  John Whaley <jwhaley@alum.mit.edu>
11   * @version $Id: PendingBuffer.java 1712 2004-04-28 17:36:13Z joewhaley $
12   */
13  class PendingBuffer {
14  
15    protected byte[] buf;
16    int    start;
17    int    end;
18  
19    int    bits;
20    int    bitCount;
21  
22    public PendingBuffer()
23    {
24      this( 4096 );
25    }
26  
27    public PendingBuffer(int bufsize)
28    {
29      buf = new byte[bufsize];
30    }
31  
32    public final void reset() {
33      start = end = bitCount = 0;
34    }
35  
36    public final void writeByte(int b) 
37    {
38      if (DeflaterConstants.DEBUGGING && start != 0)
39        throw new IllegalStateException();
40      buf[end++] = (byte) b;
41    }
42  
43    public final void writeShort(int s) 
44    {
45      if (DeflaterConstants.DEBUGGING && start != 0)
46        throw new IllegalStateException();
47      buf[end++] = (byte) s;
48      buf[end++] = (byte) (s >> 8);
49    }
50  
51    public final void writeInt(int s) 
52    {
53      if (DeflaterConstants.DEBUGGING && start != 0)
54        throw new IllegalStateException();
55      buf[end++] = (byte) s;
56      buf[end++] = (byte) (s >> 8);
57      buf[end++] = (byte) (s >> 16);
58      buf[end++] = (byte) (s >> 24);
59    }
60  
61    public final void writeBlock(byte[] block, int offset, int len) 
62    {
63      if (DeflaterConstants.DEBUGGING && start != 0)
64        throw new IllegalStateException();
65      System.arraycopy(block, offset, buf, end, len);
66      end += len;
67    }
68  
69    public final int getBitCount() {
70      return bitCount;
71    }
72  
73    public final void alignToByte() {
74      if (DeflaterConstants.DEBUGGING && start != 0)
75        throw new IllegalStateException();
76      if (bitCount > 0)
77        {
78          buf[end++] = (byte) bits;
79          if (bitCount > 8)
80            buf[end++] = (byte) (bits >>> 8);
81        }
82      bits = 0;
83      bitCount = 0;
84    }
85  
86    public final void writeBits(int b, int count)
87    {
88       if (DeflaterConstants.DEBUGGING && start != 0)
89         throw new IllegalStateException();
90       if (DeflaterConstants.DEBUGGING)
91         System.err.println("writeBits("+Integer.toHexString(b)+","+count+")");
92      bits |= b << bitCount;
93      bitCount += count;
94      if (bitCount >= 16) {
95        buf[end++] = (byte) bits;
96        buf[end++] = (byte) (bits >>> 8);
97        bits >>>= 16;
98        bitCount -= 16;
99      }
100   }
101 
102   public final void writeShortMSB(int s) {
103     if (DeflaterConstants.DEBUGGING && start != 0)
104       throw new IllegalStateException();
105     buf[end++] = (byte) (s >> 8);
106     buf[end++] = (byte) s;
107   }
108 
109   public final boolean isFlushed() {
110     return end == 0;
111   }
112 
113   /***
114    * Flushes the pending buffer into the given output array.  If the
115    * output array is to small, only a partial flush is done.
116    *
117    * @param output the output array;
118    * @param offset the offset into output array;
119    * @param length the maximum number of bytes to store;
120    * @exception IndexOutOfBoundsException if offset or length are
121    * invalid.
122    */
123   public final int flush(byte[] output, int offset, int length) {
124     if (bitCount >= 8)
125       {
126         buf[end++] = (byte) bits;
127         bits >>>= 8;
128         bitCount -= 8;
129       }
130     if (length > end - start)
131       {
132         length = end - start;
133         System.arraycopy(buf, start, output, offset, length);
134         start = 0;
135         end = 0;
136       }
137     else
138       {
139         System.arraycopy(buf, start, output, offset, length);
140         start += length;
141       }
142     return length;
143   }
144 
145   /***
146    * Flushes the pending buffer and returns that data in a new array
147    */
148 
149   public final byte[] toByteArray()
150   {
151     byte[] ret = new byte[ end - start ];
152     System.arraycopy(buf, start, ret, 0, ret.length);
153     start = 0;
154     end = 0;
155     return ret;
156   }
157   
158 }